package test_achieve;

import constant.Constant;
import constant.Pm;
import cucumber.api.java.en.Given;
import util.ConfigLog;
import util.ConfigSql;
import util.JdbcUtil;
import util.LogManager;
import util.tools.CommonTools;

/**
 * Created by chenkuo on 2018/3/15.
 */
public class LogRead {

    private String sign ="{\"date\":\"";//日志截取的标识

    @Given("^r筛选今天push_推送日志"+ Pm.p2)
    public boolean r筛选今天push_推送日志(String orderNumber,String number) throws Throwable {
        String message1 = LogManager.exeCmd(Constant.PushServerMessage1, ConfigLog.get("pushMessageOrderNum",orderNumber,CommonTools.getDate("今天"),number));
        String message2 = LogManager.exeCmd(Constant.PushServerMessage2, ConfigLog.get("pushMessageOrderNum",orderNumber,CommonTools.getDate("今天"),number));
        String message = message1+message2;
        System.out.println("-------------------push_service-message.log--------------");
        System.out.println(CommonTools.cutEnd(message,this.sign));
        boolean isPushGexin =message.contains("sendgexin");
        boolean isPushSucc =message.contains("acksucc");
        if (isPushGexin){
            System.out.println("push日志显示：订单："+orderNumber+" 推单给闪送员："+number+"，推送方式为：geixin");
        }else if (isPushSucc)
        {
            System.out.println("push日志显示：订单："+orderNumber+" 推单给闪送员："+number+"，推送方式为：acksucc");
        }else {
            System.out.println("push日志显示：订单："+orderNumber+" 没有推单给闪送员："+number);
        }
        return isPushGexin;
    }

    @Given("^r筛选今天push_推送日志"+ Pm.p1)
    public void r筛选今天push_推送日志订单号(String orderNumber) throws Throwable {
        String message1 = LogManager.exeCmd(Constant.PushServerMessage1, ConfigLog.get("pushMessageOrder",orderNumber,CommonTools.getDate("今天")));
        String message2 = LogManager.exeCmd(Constant.PushServerMessage2, ConfigLog.get("pushMessageOrder",orderNumber,CommonTools.getDate("今天")));
        String message = message1+message2;
        System.out.println("-------------------push_service-message.log--------------");
//        System.out.println(message);
        System.out.println(this.sign+CommonTools.cutEnd(message,this.sign));
    }


    @Given("^r筛选今天distribute_调度日志"+ Pm.p1)
    public void r筛选今天distribute_调度日志(String orderNumber) throws Throwable {
        String message = LogManager.exeCmd(Constant.PushServerMessage1, ConfigLog.get("distributeOrder",orderNumber,CommonTools.getDateHour()));
//        System.out.println(message);
        System.out.println("-------------------distribute_service- dispatch.log--------------");
        System.out.println(this.sign+CommonTools.cutEnd(message,this.sign));
    }

    @Given("^r筛选今天distribute_调度日志"+ Pm.p2)
    public boolean r筛选今天distribute_调度日志(String orderNumber,String num) throws Throwable {
        String message = LogManager.exeCmd(Constant.PushServerMessage1, ConfigLog.get("distributeOrderNum",orderNumber,CommonTools.getDateHour(),num));
        System.out.println("-------------------distribute_service- dispatch.log--------------");
//        System.out.println(message);
        System.out.println(this.sign+CommonTools.cutEnd(message,this.sign));
        boolean isPushChannel =message.contains("couriers pushChannel filter false");
        if (isPushChannel){
            System.out.println("订单："+orderNumber+" 未推单给闪送员："+num+"，原因：push通道不通");
        }
        else {
            System.out.println("因其它原因导致未推送，请跟进日志分析");
        }
        return isPushChannel;
    }

    @Given("^r筛选今天distribute_调度日志推送人员"+ Pm.p1)
    public void r筛选今天distribute_调度日志推送人员(String orderNumber) throws Throwable {
        String message = LogManager.exeCmd(Constant.PushServerMessage1, ConfigLog.get("distributeOrderMess",orderNumber,CommonTools.getDateHour(),"finished pushTasks orderNumber"));
        System.out.println("-------------------distribute_service- dispatch.log--------------");
//        System.out.println(message);
        System.out.println(this.sign+CommonTools.cutEnd(message,this.sign));
    }

    @Given("^r筛选今天distribute_调度日志派单排序与推荐值"+ Pm.p1)
    public void r筛选今天distribute_调度日志派单排序与推荐值(String orderNumber) throws Throwable {
        String message = LogManager.exeCmd(Constant.PushServerMessage1, ConfigLog.get("distributeOrderNum",orderNumber,CommonTools.getDateHour(),"couriers list and recommendValue"));
        System.out.println("-------------------distribute_service- dispatch.log--------------");
//        System.out.println(message);
        System.out.println(this.sign+CommonTools.cutEnd(message,this.sign));
    }

    @Given("^r筛选今天distribute日志"+ Pm.p1)
    public void r筛选今天distribute日志(String orderNumber) throws Throwable {
        String message = LogManager.exeCmd(Constant.PushServerMessage1, "grep \""+orderNumber+"\" /mnt/iss/service/distribute/nlogs/dispatch.2018-04-04-11.log");
        System.out.println("-------------------push_service-message.log--------------");
        System.out.println(message);

    }
}
